#include "stdafx.h"
#include "launcher.h"
#include "MyGLWidget.h"
#include "QtLogger.h"
#include "App/AppBase.h"

using namespace RayFrame;

Launcher::Launcher(QWidget *parent, Qt::WFlags flags)
	: QMainWindow(parent, flags), dlgCameraPosition(NULL)
{
	ui.setupUi(this);

	glWidget = new MyGLWidget(this);
  glWidget->RegisterApp(g_App);

	setCentralWidget(glWidget);

  logger = new QtLogger(ui.textEditLog);
  RayFrame::Logger::InstanceRef().AddPolicy("dummy", logger);
  
  ui.comboBoxLogLevel->setCurrentIndex(1);
}

Launcher::~Launcher()
{
  // disable the logger so it won't try insert text info text edit widget.
  logger->Enable(false);
}

void Launcher::setupSignal()
{

}

void Launcher::on_actionExit_triggered()
{
	this->close();
}

void Launcher::on_actionSet_camera_position_triggered()
{
  if(!dlgCameraPosition)
  {
    dlgCameraPosition = new QDialog();
    dlgCameraPosition->setModal(true);
    uiDlg.setupUi(dlgCameraPosition);
    connect(uiDlg.btnCancel, SIGNAL(clicked()), this, SLOT(on_camera_position_cancel()));
    connect(uiDlg.btnOK, SIGNAL(clicked()), this, SLOT(on_camera_position_ok()));
    connect(uiDlg.btnReset, SIGNAL(clicked()), this, SLOT(on_camera_position_reset()));
  }

  glm::vec3 eye = RayFrame::g_App->GetCamera()->GetEye();
  glm::vec3 center = RayFrame::g_App->GetCamera()->GetCenter();

  uiDlg.posx->setValue(eye.x);
  uiDlg.posy->setValue(eye.y);
  uiDlg.posz->setValue(eye.z);

  uiDlg.targetx->setValue(center.x);
  uiDlg.targety->setValue(center.y);
  uiDlg.targetz->setValue(center.z);
  
  dlgCameraPosition->show();
}

void Launcher::on_camera_position_cancel()
{
  dlgCameraPosition->hide();
}

void Launcher::on_camera_position_ok()
{
  glm::vec3 pos( uiDlg.posx->value(), uiDlg.posy->value(), uiDlg.posz->value() );
  glm::vec3 target( uiDlg.targetx->value(), uiDlg.targety->value(), uiDlg.targetz->value() );

  RayFrame::g_App->GetCamera()->LookAt(pos, target);
  dlgCameraPosition->hide();
}

void Launcher::on_camera_position_reset()
{
  g_App->GetCamera()->Reset();
}

void Launcher::on_comboBoxLogLevel_currentIndexChanged( QString str )
{
  LogLevel ll;
  if(str == "Higher")
    ll = LogHigher;
  else if(str == "Normal")
    ll = LogNormal;
  else
    ll = LogLower;
  
  Logger::InstanceRef().SetFilter(ll);
}

void Launcher::Enable( bool e )
{
  logger->Enable(e);
}

void Launcher::on_actionLogHigher_triggered()
{
  ui.comboBoxLogLevel->setCurrentIndex(2);
  ui.actionLogNormal->setChecked(false);
  ui.actionLogLower->setChecked(false);
}

void Launcher::on_actionLogLower_triggered()
{
  ui.comboBoxLogLevel->setCurrentIndex(0);
  on_comboBoxLogLevel_currentIndexChanged("Lowerer");
  ui.actionLogNormal->setChecked(false);
  ui.actionLogHigher->setChecked(false);
}

void Launcher::on_actionLogNormal_triggered()
{
  ui.comboBoxLogLevel->setCurrentIndex(1);
  on_comboBoxLogLevel_currentIndexChanged("Normal");
  ui.actionLogHigher->setChecked(false);
  ui.actionLogLower->setChecked(false);
}

RayFrame::FrameBase* Launcher::GetFrame()
{
  return glWidget;
}

void Launcher::on_pushButtonConsoleEnter_clicked()
{
  QString cmd = ui.lineEditConsoleInput->text();
  if(cmd.isEmpty())
    return;
  
  ui.textEditConsoleOutput->append(cmd);
  ui.lineEditConsoleInput->clear();
}
